<?php
namespace App\Http\Controllers\Admin\Yqh;

use App\Service\Admin\Yqh\YqhService;
use App\Utility\Upload;
use Illuminate\Http\Request;

class YqhController extends BaseController
{
    //邀请函

    //列表
    public function index(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $res = YqhService::index($pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //修改
    public function modify(Request $request)
    {
        $id = $request->post('id', 0);
        $pas['title'] = $request->post('title', '');
        $pas['invited'] = $request->post('invited', '');
        $pas['canjia_time'] = $request->post('canjia_time', '');
        $pas['canjia_dizhi'] = $request->post('canjia_dizhi', '');
        $bg = $request->post('bg', '');
        if ($bg) {
            if (substr($bg, 0, 5) == 'data:') {
                $pas['bg'] = Upload::getPathByBase64($bg);
            } else {
                $pas['bg'] = $bg;
            }
        }
        if ($id) {
            $res = YqhService::modify($id, $pas);
        } else {
            $res = YqhService::add($pas);
        }
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function del(Request $request)
    {
        $id = $request->post('id', 0);
        $res = YqhService::del($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}